# Maintainer: Johannes Schindelin <johannes.schindelin@gmx.de>

_realname=7zip
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=24.09
pkgrel=3
pkgdesc="A file archiver with a high compression ratio (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://7-zip.org"
msys2_references=(
  'aur: 7-zip'
  "cpe: cpe:/a:7-zip:7-zip"
)
license=('spdx:LGPL-2.1-or-later AND BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
source=("$url/a/7z${pkgver//./}-src.tar.xz"
        001-disable-static.patch
        002-guard-msvc-code.patch)
sha256sums=('49c05169f49572c1128453579af1632a952409ced028259381dac30726b6133a'
            '80678c664cefb38e6402c83fc0878911c142e471ce42665291bb313d8c8e5185'
            '6e2898dd658d760cf82b591d90031cd06cf670714e611a9d5b662bb25f426ded')

prepare () {
  if [[ ${CC} == clang ]] ; then
    sed -i 's/-Werror/ -Wno-missing-exception-spec -Wno-unused-but-set-variable -Wno-unknown-attributes/' */*.mak */*/*.mak
  fi
  patch -p1 -i "${srcdir}"/001-disable-static.patch

  # https://sourceforge.net/p/sevenzip/bugs/2530/
  patch -p1 -i "${srcdir}"/002-guard-msvc-code.patch
}

build() {
  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  mkdir -p build-${MSYSTEM}
  cp -r CPP build-${MSYSTEM}/CPP
  cp -r C build-${MSYSTEM}/C
  cd build-${MSYSTEM}

  make -C CPP/7zip/Bundles/Format7zF -f makefile.gcc
  make -C CPP/7zip/UI/Console -f makefile.gcc
  # undo 001-disable-static.patch for SFXCon in particular, it should be
  # standalone
  make -C CPP/7zip/Bundles/SFXCon -f makefile.gcc LDFLAGS_STATIC_2=-static
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  install -D -m755 $(find CPP/7zip/Bundles/ -name \*.dll) -t "${pkgdir}"${MINGW_PREFIX}/bin/
  install -D -m755 CPP/7zip/UI/Console/_o/7z.exe "${pkgdir}"${MINGW_PREFIX}/bin/7z.exe
  install -D -m755 CPP/7zip/Bundles/SFXCon/_o/7zCon.exe "${pkgdir}"${MINGW_PREFIX}/lib/7zip/7zCon.sfx

  install -d "${pkgdir}"${MINGW_PREFIX}/share/doc/7zip
  install -D -m644 "${srcdir}"/DOC/* "${pkgdir}"${MINGW_PREFIX}/share/doc/7zip
}
